//
//  WBEmoticonKeyBoardView.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/23.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//
#import "WBEmoticonKeyBoardView.h"
#import "WBEmoticonToolBar.h"
#import <Masonry.h>

#import "WBEmoticonManager.h"
#import "WBEmoticonCollectionViewCell.h"



@interface WBEmoticonKeyBoardView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic ,strong) UICollectionView *emoticonCollection;

@property(nonatomic ,strong) WBEmoticonToolBar *emoticonToolBar;

@property(nonatomic ,strong) UIPageControl *pageControl;

@property(nonatomic ,strong) UILabel *recentLable;

@end


static NSString *emoticonID = @"emoticonID";
static CGFloat toolBarH = 37;

@implementation WBEmoticonKeyBoardView

#pragma mark : - 懒加载
-(WBEmoticonToolBar *)emoticonToolBar{

    if (nil == _emoticonToolBar) {
        
        _emoticonToolBar = [[WBEmoticonToolBar alloc]init];
        _emoticonToolBar.axis = UILayoutConstraintAxisHorizontal;
        _emoticonToolBar.distribution = UIStackViewDistributionFillEqually;
    }
    
    return _emoticonToolBar;
}

-(UICollectionView *)emoticonCollection{

    if (nil == _emoticonCollection) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.itemSize = CGSizeMake(kUISCRRENW, self.bounds.size.height - toolBarH);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _emoticonCollection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        
        _emoticonCollection.dataSource = self;
        _emoticonCollection.delegate = self;
        
        _emoticonCollection.bounces = NO;
        _emoticonCollection.pagingEnabled = YES;
        _emoticonCollection.backgroundColor = [UIColor whiteColor];
        
        [_emoticonCollection registerClass:[WBEmoticonCollectionViewCell class] forCellWithReuseIdentifier:emoticonID];
        
    }
    
    return _emoticonCollection;
}

-(UIPageControl *)pageControl{

    if (nil == _pageControl) {
        
        _pageControl = [[UIPageControl alloc]init];
        
        _pageControl.numberOfPages = 5;
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
        
        //自定义page视图
        [_pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKey:@"_currentPageImage"];
        
        [_pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKey:@"_pageImage"];
    }
    return _pageControl;
}

-(UILabel *)recentLable{

    if (nil == _recentLable) {
        
        _recentLable = [UILabel cz_labelWithText:@"最近使用的表情" fontSize:10 color:[UIColor orangeColor]];
        [_recentLable sizeToFit];
    }
    
    return _recentLable;
}




-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI{

//    self.backgroundColor = [UIColor redColor];
 
    [self addSubview:self.emoticonToolBar];
    
    [_emoticonToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.offset(toolBarH);
    }];
    
    
    [self addSubview:self.emoticonCollection];
    
    [self.emoticonCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(self.emoticonToolBar.mas_top);
    }];
    
    [self addSubview:self.pageControl];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.emoticonToolBar.mas_top);
    }];
    
    [self addSubview:self.recentLable];
    
    [self.recentLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.center.equalTo(self.pageControl);
    }];
    
    
    
    __weak typeof(self) weakSelf = self;
    self.emoticonToolBar.emoticonClickBlock = ^(UIButton *btn){
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:btn.tag];
        
        [weakSelf.emoticonCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        //跟新pagecontroll
        [weakSelf updatePageControlData:indexPath];
    };
    
    //设置UI界面的时候 就手动调用更新数据的方法
    //主队列异步 实际上还是主队列 当前任务的优先级是比较低优先级 等到主队列空闲的时候 才会执行代码块中的任务
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self updatePageControlData:[NSIndexPath indexPathForItem:0 inSection:0]];
    });
    
    //监听通知
    [self registerNotification];
}

-(void)registerNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateRecentEmoticons) name:@"KsaveRecentEmoticon" object:nil];
    
}

#pragma mark : - 刷新第0组
-(void)updateRecentEmoticons{
  
    //刷新第0组
    //如果当前显示的就是第0组 就不执行刷新
    NSIndexPath *indexPath = [[self.emoticonCollection indexPathsForVisibleItems] lastObject];
    if (indexPath.section != 0) {
    
        [self.emoticonCollection reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]];
    }
}

/// 根据indexPath 更新pageControl的显示数据
///
/// - parameter indexPath: 索引对象
-(void)updatePageControlData:(NSIndexPath *)indexPath {

    //1. 根据indexPath.section  --> 获取到allEmoticons对应的二维数组
    NSArray *array = (NSArray *)[WBEmoticonManager shared].allEmoticons[indexPath.section];
    
    self.pageControl.numberOfPages = array.count;
    self.pageControl.currentPage = indexPath.item;
    
    //设置最近文本的显示 和 pageControl的显示
    self.pageControl.hidden = indexPath.section == 0;
    self.recentLable.hidden = indexPath.section != 0;
}

#pragma mark : - UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return [WBEmoticonManager shared].allEmoticons.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSArray *sectionArray = (NSArray *)[WBEmoticonManager shared].allEmoticons[section];
    return sectionArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    WBEmoticonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:emoticonID forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor cz_randomColor];
    NSArray *emoticonArray = (NSArray *)[WBEmoticonManager shared].allEmoticons[indexPath.section][indexPath.row];
    
    cell.emotionsArray = emoticonArray;
    return cell;
}


#pragma mark : - UICollectionViewDelegate 手动拖拽

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat contentOffsetX = scrollView.contentOffset.x + kUISCRRENW / 2;
    
    CGPoint point = CGPointMake(contentOffsetX, 1);
    //根据一个点来确定indexPath
    NSIndexPath *indexPath = [self.emoticonCollection indexPathForItemAtPoint:point];
    //让toolBar去更新选中的按钮
    [self.emoticonToolBar setEmoticonTypeSelected:indexPath];
    
    [self updatePageControlData:indexPath];
}






























@end
